package algorithms.sort.nlogn;

import algorithms.sort.SortingVisualization;

/**
 * 希尔排序
 */
public class ShellSort extends SortingVisualization {

    /**
     * 初始化窗口大小并
     *
     * @param array
     */
    public ShellSort(int[] array) {
        super(array);
    }

    @Override
    public void showSortingProcess() {
        shellSort(array);
    }

    public void shellSort(int[] array) {
        int n = array.length;
        int gap = n / 2;

        while (gap > 0) {
            for (int i = gap; i < n; i++) {
                int temp = array[i];
                int j = i;

                while (j >= gap && array[j - gap] > temp) {
                    array[j] = array[j - gap];
                    j -= gap;
                    sortPanelRepaint();
                }

                array[j] = temp;
                sortPanelRepaint();
            }

            gap /= 2;
        }
    }
}
